package com.example.manager.service;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.manager.entity.Admin;
import com.example.manager.entity.Student;
import com.example.manager.entity.Teacher;
import com.example.manager.repository.AdminRepository;
import com.example.manager.repository.StudentRepository;
import com.example.manager.repository.TeacherRepository;

@Service
@Transactional
public class LoginServiceImpl implements LoginService{
	
	@Autowired
	private StudentRepository studentRepository;
	
	@Autowired
	private AdminRepository adminRepository;
	
	@Autowired
	private TeacherRepository teacherRepository;

	@Override
	public String login(String type, String id, String password, HttpSession session) {
		if(type.equals("student")) {
			Student stu  = studentRepository.getBySidAndPassword(id,password);
			if(stu!=null) {
				session.setAttribute("sid", id);
				return "student";
			}else {
				return "Error";
			}
		}
		else if(type.equals("teacher")) {
			Teacher teacher = teacherRepository.getByTidAndPassword(id, password);
			if(teacher!=null) {
				session.setAttribute("tid", id);
				return "teacher";
			}else return "Error";
		}
		else if(type.equals("admin")){
			Admin admin = adminRepository.getByIdAndPassword(Integer.parseInt(id), password);
			if(admin!=null) {
				session.setAttribute("aid", id);
				return "admin";
			}else return "er";
			
			
		}
		return "no ok!";
	}

}
